package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.query.BpmnInfoQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.util.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.image.ProcessDiagramGenerator;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class BpmnInfoServiceImpl  extends
        ServiceImpl<BpmnInfoMapper,BpmnInfo>implements IBpmnInfoService {
    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;

    @Override
    public TablePageInfo<BpmnInfo> query(BpmnInfoQuery qo) {
        Page<BpmnInfo> page=new Page<>(qo.getPageNum(),qo.getPageSize());
       QueryWrapper<BpmnInfo> wrapper=new QueryWrapper<>();
        return new TablePageInfo<BpmnInfo>(super.page(page,wrapper));
    }



    @Override
    public void delete(Long id) {
        //通过id去bus_bpmn_info表里面查出数据
        BpmnInfo bpmnInfo = this.getById(id);
        //删除bus_bpmn_info表中的数据
        bpmnInfoMapper.deleteById(id);
        //通过流程定义对象,删除activiti里面的数据
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(bpmnInfo.getDeploymentId()).singleResult();
        repositoryService.deleteDeployment(bpmnInfo.getDeploymentId(),true);
        //删除电脑上面的文件
        String resourceName = processDefinition.getResourceName();
        File file = new File(SystemConfig.getProfile(), resourceName);
        //判断下file里面是否有东西
        if (file.exists()){//判断文件是否存在的意思,存在返回true
            file.delete();
        }

    }


    @Override
    public void depoly(String bpmnInfoPath, String bpmnType, String info) throws FileNotFoundException {

        Deployment deploy=null;
        //创建流程定义对象
         deploy=repositoryService.createDeployment().addInputStream(bpmnInfoPath,
                 new FileInputStream(new File(SystemConfig.getProfile()
                         ,bpmnInfoPath)))
                 .deploy();
        BpmnInfo bpmnInfo = new BpmnInfo();
        //将获取到的流程实例的数据设置进数据库中
        bpmnInfo.setBpmnType(bpmnType);
        bpmnInfo.setDeployTime(new Date());
        bpmnInfo.setDeploymentId(deploy.getId());
        bpmnInfo.setInfo(info);
        //创建流程实例对象
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deploy.getId())
                .singleResult();
        //将获取到的流程实例的数据设置进数据库中
        bpmnInfo.setBpmnName(processDefinition.getName());
        bpmnInfo.setActProcessId(processDefinition.getId());
        bpmnInfo.setActProcessKey(processDefinition.getKey());
        bpmnInfoMapper.insert(bpmnInfo);

    }

    @Override
    public InputStream getResoucceByType(String deploymentId, String type) {
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deploymentId)
                .singleResult();
        if ("xml".equalsIgnoreCase(type)){
            return repositoryService.getResourceAsStream(deploymentId
                    ,processDefinition.getResourceName());
        }else if ("png".equalsIgnoreCase(type)){
            BpmnModel bpmnModel = repositoryService
                    .getBpmnModel(processDefinition.getId());
            ProcessDiagramGenerator generator=new
                    DefaultProcessDiagramGenerator();
            //generateDiagram(流程模型,需要高亮的节点,需要高亮的线条,后面三个参数都表示是字体)
            InputStream inputStream = generator.generateDiagram(bpmnModel, Collections.EMPTY_LIST, Collections.EMPTY_LIST
                    , "宋体", "宋体", "宋体");
            return inputStream;
        }
        return null;
    }

    @Override
    public BpmnInfo queryByType(String car_package) {
        QueryWrapper<BpmnInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("bpmn_type",car_package);
        return bpmnInfoMapper.selectOne(wrapper);
    }
}
